<template>
  <div class="product-edit">
    <el-form :model="productForm" label-width="120px">
      <el-form-item label="产品名称">
        <el-input
          v-model="productForm.name"
          placeholder="请输入产品名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="产品价格">
        <el-input-number
          v-model="productForm.price"
          :min="0"
          placeholder="请输入产品价格"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="域名">
        <el-input
          type="textarea"
          v-model="productForm.domains"
          placeholder="请输入域名，每行一个"
          :rows="5"
        ></el-input>
      </el-form-item>
      <el-form-item label="国家">
        <el-select v-model="productForm.country" placeholder="选择国家">
          <el-option label="美国" value="美国"></el-option>
          <el-option label="新加坡" value="新加坡"></el-option>
          <el-option label="中国" value="中国"></el-option>
          <!-- 可以根据需要添加更多国家 -->
        </el-select>
      </el-form-item>
      <el-form-item label="产品状态">
        <el-switch
          v-model="productForm.isActive"
          active-text="上架"
          inactive-text="下架"
        ></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveProduct">保存</el-button>
        <el-button @click="cancelEdit">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import * as productApi from "../../../api/project3/product/product.js";

const router = useRouter();
const productForm = ref({
  id: null,
  name: "",
  price: 0.0,
  domains: "",
  country: "",
  isActive: 1,
});

const saveProduct = async () => {
  try {
    const res = await productApi.saveOrUpdate(productForm.value);
    if (res.code === "200") {
      ElMessage.success("保存成功");
      router.push({ name: "ProductList" });
    } else {
      ElMessage.error(res.msg);
    }
  } catch (error) {
    ElMessage.error("保存失败");
  }
};

const cancelEdit = () => {
  router.push({ name: "ProductList" });
};
</script>

<style scoped>
.product-edit {
  padding: 20px;
}
</style> 